package com.ycky.starlink.controller.pms;


import com.ycky.common.core.controller.BaseController;
import com.ycky.common.core.domain.AjaxResult;
import com.ycky.common.core.page.TableDataInfo;
import com.ycky.starlink.domain.entity.pms.PmsProductCategory;
import com.ycky.starlink.domain.param.PmsProductCategoryParam;
import com.ycky.starlink.domain.param.PmsProductCategoryWithChildrenItem;
import com.ycky.starlink.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 商品分类管理
 * @author: cjj
 * @date: 2025/3/25 17:32
 **/
@RestController
@Api(tags = "PmsProductCategoryController")
@Tag(name = "PmsProductCategoryController", description = "商品分类管理")
@RequestMapping("/productCategory")
public class PmsProductCategoryController extends BaseController {
    @Autowired
    private PmsProductCategoryService productCategoryService;

    @ApiOperation("分页查询商品分类")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo getList(@PathVariable Long parentId,
                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        startPage();
        List<PmsProductCategory> list = productCategoryService.getList(parentId, pageSize, pageNum);
        return getDataTable(list);
    }

    @ApiOperation("查询所有一级分类及子分类")
    @RequestMapping(value = "/list/withChildren", method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo listWithChildren() {
        startPage();
        List<PmsProductCategoryWithChildrenItem> list = productCategoryService.listWithChildren();
        return getDataTable(list);
    }

    @ApiOperation("添加商品分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam) {
        int count = productCategoryService.create(productCategoryParam);
        if (count > 0) {
            return AjaxResult.success(count);
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("修改商品分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@PathVariable Long id,
                             @Validated
                             @RequestBody PmsProductCategoryParam productCategoryParam) {
        int count = productCategoryService.update(id, productCategoryParam);
        if (count > 0) {
            return AjaxResult.success(count);
        } else {
            return AjaxResult.error();
        }
    }


    @ApiOperation("根据id获取商品分类")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getItem(@PathVariable Long id) {
        PmsProductCategory productCategory = productCategoryService.getItem(id);
        return AjaxResult.success(productCategory);
    }

    @ApiOperation("删除商品分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id) {
        int count = productCategoryService.delete(id);
        if (count > 0) {
            return AjaxResult.success(count);
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("修改导航栏显示状态")
    @RequestMapping(value = "/update/navStatus", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus) {
        int count = productCategoryService.updateNavStatus(ids, navStatus);
        if (count > 0) {
            return AjaxResult.success(count);
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("修改显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        int count = productCategoryService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return AjaxResult.success(count);
        } else {
            return AjaxResult.error();
        }
    }

}
